import { MapData, Block } from "../Data/MapData";
import CharView from "./CharView";
import MapView from "./MapView";

// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class WaitSelectView extends cc.Component {

    @property(cc.Prefab)
    charPrefab: cc.Prefab = null;

    // LIFE-CYCLE CALLBACKS:

    static instance : WaitSelectView = null;

    onLoad () {
        WaitSelectView.instance = this;
    }

    CreateCharView(row:number, col:number, parent:cc.Node):CharView{
        let mapData = MapData.GetInstance().data; // shortcut
        let block : Block = mapData[row][col];
        let charNode : cc.Node = cc.instantiate(this.charPrefab);
        charNode.setParent(this.node);

        let waitView : CharView = charNode.getComponent(CharView);

        // 建立单向关联
        //block.waitView = waitView;
        waitView.blockData = block;

        waitView.char = block.char;
        waitView.isGray = false;
        waitView.isHidden = false;
        waitView.isWaitSelect = true;

        return waitView;
    }

    start() {
        let mapData = MapData.GetInstance().data; // shortcut
        let ps = MapData.GetInstance().hiddenPoses; // shortcut

        for (let i = 0; i < ps.length; i++) {
            let pos = ps[i];

            // 等待区文字和地图区文字关联同一个BlockData
            let waitCharView = this.CreateCharView(pos.top,pos.left,this.node);

            waitCharView.node.on(cc.Node.EventType.MOUSE_UP, () => {
                if (MapView.instance.selCharView.isCharFlipped) {
                    return;
                }
                // 数据层要更新属性
                MapView.instance.selCharView.blockData.isCharFlipped = true;
                MapView.instance.selCharView.blockData.char = waitCharView.blockData.char;

                // 界面层要更新显示
                MapView.instance.selCharView.isCharFlipped = true;
                MapView.instance.selCharView.char = waitCharView.char;

                waitCharView.isCharFlipped = true;

                // 1. 判断是否猜对整个成语
                for (let iWord = 0; iWord < waitCharView.blockData.wordIndices.length; iWord++) {
                    let iWordIndex = waitCharView.blockData.wordIndices[iWord];
                    let myWord = MapData.GetInstance().myLevel.words[iWordIndex];

                    let isIdiomFlipped = true; // 是否整个成语被翻开
                    for (let iBlock = 0; iBlock < myWord.blocks.length; iBlock++) {
                        let myBlock = myWord.blocks[iBlock];

                        // 用排除法，如果一个暗字没有被翻开，说明没有猜完整个成语
                        if (myBlock.isHidden && !myBlock.isCharFlipped) {
                            isIdiomFlipped = false;
                            break;
                        }
                    }

                    if (isIdiomFlipped) {
                        let isIdiomRight = true;

                        for (let iBlock = 0; iBlock < myWord.blocks.length; iBlock++) {
                            let myBlock = myWord.blocks[iBlock];

                            // 用排除法，如果一个暗字没有被翻对，说明没有猜对整个成语
                            if (myBlock.char != myBlock.mapCharView.char) {
                                isIdiomRight = false;
                                break;
                            }
                        }

                        if (isIdiomRight) {
                            for (let iBlock = 0; iBlock < myWord.blocks.length; iBlock++) {
                                let myBlock = myWord.blocks[iBlock];

                                myBlock.isIdiomFlipped = true;
                                myBlock.mapCharView.isIdiomFlipped = true;
                            }
                            
                            // TODO: 跳到下个汉字
                        }
                    }
                }
            });
        }
    }

    // update (dt) {}
}
